package com.neulogistics.core.substation.api.application.command.handler;

import com.blackto.lib.mediat.unicast.IRequestHandler;
import com.neulogistics.core.substation.api.application.command.CreateTaskOrderCommand;
import com.neulogistics.core.substation.api.application.query.ISubstationQueryService;
import com.neulogistics.core.substation.api.application.rpc.ISubstationRPC;
import com.neulogistics.core.substation.api.application.rpc.dto.OrderDTO;
import com.neulogistics.core.substation.domain.model.aggregate.Substation;
import com.neulogistics.core.substation.domain.model.aggregate.TaskOrder;
import com.neulogistics.core.substation.domain.model.enumeration.TaskType;
import com.neulogistics.core.substation.domain.model.valueobject.CustomInfo;
import com.neulogistics.core.substation.domain.model.valueobject.OrderInfo;
import com.neulogistics.core.substation.domain.repository.ISubstationRepository;
import com.neulogistics.core.substation.domain.repository.ITaskOrderRepository;
import com.neulogistics.infrastructure.cache.idservice.RedisIdUtil;
import com.neulogistics.infrastructure.common.serviceresult.BaseResponse;
import com.neulogistics.infrastructure.common.serviceresult.ResultUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class CreateTaskOrderCommandHandler implements IRequestHandler<CreateTaskOrderCommand, BaseResponse<String>> {
    @Resource
    ISubstationRPC substationRPC;

    @Resource
    RedisIdUtil redisIdUtil;

    @Resource
    ISubstationQueryService substationQueryService;

    @Resource
    ITaskOrderRepository taskOrderRepository;

    @Value("${spring.application.name}")
    private String appName;

    private final String Task_Order_Id_Key;

    public CreateTaskOrderCommandHandler(@Value("${spring.application.name}") String appName) {
        Task_Order_Id_Key = appName + ":" + "taskOrderId";
    }


    @Override
    public BaseResponse<String> handle(CreateTaskOrderCommand command) {
        // 查分站id
        Substation substation = substationQueryService.getSubstationByWarehouseId(command.subWarehouseId);
        Integer substationId = substation.getSubstationId();

        // 查订单信息
        OrderDTO orderDTO = substationRPC.getOrderById(command.orderId);
        OrderInfo orderInfo = new OrderInfo();
        BeanUtils.copyProperties(orderDTO, orderInfo);

        TaskType taskType = TaskType.fromValue(orderDTO.orderType, TaskType.class);

        // 查客户信息
        CustomInfo customInfo = substationRPC.getCustomerById(orderDTO.customerId);

        // 生成任务单id
        Long taskId = redisIdUtil.nextId(Task_Order_Id_Key);

        // 创建任务单
        TaskOrder taskOrder = new TaskOrder(taskId, taskType, substationId, orderInfo, customInfo, null);

        // 持久化
        taskOrderRepository.save(taskOrder);

        return ResultUtils.success(taskId.toString());
    }
}
